package gui.tab;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Observable;
import java.util.Observer;

import gui.TuristAssistant;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import org.example.www.webSuscriptionService.Feed;

public class SuscriptionCompositeTab extends org.eclipse.swt.widgets.Composite implements Observer {
	private ExpandBar barSuscription;
	private CCombo cComboSuscriptions;
	private Button buttonUpdate;
	
	private Hashtable<String,Integer> suscriptions = new Hashtable<String,Integer>();

	public SuscriptionCompositeTab(org.eclipse.swt.widgets.Composite parent,
			int style) {
		super(parent, style);
		TuristAssistant.suscriptionUpdater.addObserver(this);
		initGUI();
		
	}

	private void initGUI() {
		try {
			GridLayout thisLayout = new GridLayout();
			thisLayout.numColumns = 3;
			this.setLayout(thisLayout);
			this.setSize(380, 217);
			{
				cComboSuscriptions = new CCombo(this, SWT.BORDER);
				GridData cComboSuscriptionsLData = new GridData();
				cComboSuscriptionsLData.grabExcessHorizontalSpace = true;
				cComboSuscriptionsLData.horizontalAlignment = GridData.FILL;
				cComboSuscriptions.setLayoutData(cComboSuscriptionsLData);
				cComboSuscriptions.setText("<Select a suscription>");
			}
			{
				buttonUpdate = new Button(this, SWT.PUSH | SWT.CENTER);
				buttonUpdate.setText("Update");
				buttonUpdate.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						buttonUpdateMouseUp(evt);
					}
				});
			}
			{
				GridData barSuscriptionLData = new GridData();
				barSuscriptionLData.horizontalSpan = 3;
				barSuscriptionLData.grabExcessHorizontalSpace = true;
				barSuscriptionLData.horizontalAlignment = GridData.FILL;
				barSuscriptionLData.verticalAlignment = GridData.FILL;
				barSuscriptionLData.grabExcessVerticalSpace = true;
				barSuscription = new ExpandBar(this, SWT.V_SCROLL);
				barSuscription.setLayoutData(barSuscriptionLData);
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

/**
 * Agrega un nuevo {@link Feed}. (Noticia)
 * @param feed
 * 				{@link Feed} a agregar en la interfaz.
 */
	public void addSuscription(Feed feed) {
		Composite composite = new Composite(barSuscription, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		layout.verticalSpacing = 10;
		composite.setLayout(layout);
		
		
		StyledText info = new StyledText(composite, SWT.V_SCROLL | SWT.WRAP | SWT.BORDER);
		GridData styledTextLData = new GridData();
		styledTextLData.horizontalSpan = 2;
		styledTextLData.grabExcessHorizontalSpace = true;
		styledTextLData.horizontalAlignment = GridData.FILL;
		styledTextLData.verticalAlignment = GridData.FILL;
		styledTextLData.grabExcessVerticalSpace = true;
		styledTextLData.heightHint = 100;
		info.setLayoutData(styledTextLData);
		

		info.setText(feed.getBody());
		info.setJustify(true);
		ExpandItem item = new ExpandItem(barSuscription, SWT.NONE, barSuscription.getItemCount());
		item.setText(feed.getTitle());
		item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item.setControl(composite);
		item.setImage(this.getDisplay().getSystemImage(SWT.ICON_INFORMATION));
	}
	
	private void buttonUpdateMouseUp(MouseEvent evt) {
		getSuscriptionsFrom(cComboSuscriptions.getText());
	}

	/**
	 * Baja los nuevos {@link Feed} del servidor.
	 * @param url
	 * 				url del feed's server.
	 */
	public void getSuscriptionsFrom(String url) {
		try {
			if(!suscriptions.containsKey(new String(url))){
				int serial = TuristAssistant.suscriptionClient.submit(url);
				suscriptions.put(url, serial);
			}
			ArrayList<Feed> feeds = TuristAssistant.suscriptionClient.getFeed(url, suscriptions.get(url));
			
			suscriptions.put(url, suscriptions.get(url)+ feeds.size());

			for(Feed current : feeds){
				addSuscription(current);
			}
			addSuscriptionFeed(url);
		} catch (Exception e) {
			MessageBox dialog = new MessageBox(this.getShell(),SWT.ICON_ERROR|SWT.OK);
			dialog.setMessage("Can not reach "+ url+".\nPlease be sure that the direction is correct.");
			dialog.setText("Suscription feed not found");
			dialog.open();
		}
		
	}

	private void addSuscriptionFeed(String url) {
		String[] items = cComboSuscriptions.getItems();
		for(String current : items){
			if(current.equals(url))
				return;
		}
		cComboSuscriptions.add(url);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		String url = (String) arg1;
		getSuscriptionsFrom(url);
		
	}

}
